package com.jy.appmarket.cards;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

import android.content.Context;

import com.jy.appmarket.utils.LogUtil;

import dalvik.system.DexFile;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2015年8月21日
 * @version 1.0
 */

public final class CardsChecker {
	public static boolean check(Context context) {
		if (context == null) {
			return false;
		}
		String prefix = context.getPackageName() + ".cards";
		Set<Integer> types = new HashSet<Integer>();
		try {
			DexFile df = new DexFile(context.getPackageCodePath());
			for (Enumeration<String> iter = df.entries(); iter.hasMoreElements();) {
				String s = iter.nextElement();
				if (!s.startsWith(prefix)) {
					continue;
				}
				if (s.endsWith("UICard")) {
					Class<?> cls = Class.forName(s);
					Method getType = cls.getDeclaredMethod("getType");
					Constructor<?> c = cls.getConstructor(Context.class);
					Object obj = c.newInstance(context);
					int type = (Integer) getType.invoke(obj);
					if (!types.add(type)) {
						LogUtil.w("CardsChecker", "type conflict:" + s);
					}
					LogUtil.d("CardsChecker", "cls:" + s + " " + type);
				}
			}
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return false;

	}
}